﻿using FluentNHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;

namespace AcceptanceTests
{
	public class TestModel : PersistenceModel
	{
		public TestModel()
		{
			AddMappingsFromAssembly(typeof(ProjectMap).Assembly);
		}
	}

	[TestClass]
	public class PersistanceTests
	{
		public TestContext TestContext { get; set; }
		protected SessionSource SessionSource { get; set; }
		protected ISession Session { get; private set; }

		[ClassInitialize]
		public static void MyClassInitialize(TestContext testContext)
		{
		}

		[ClassCleanup]
		public static void MyClassCleanup()
		{
		}

		[TestInitialize]
		public void MyTestInitialize()
		{
			var cfg = Fluently.Configure()
				.Database(SQLiteConfiguration.Standard.ShowSql().InMemory);
			SessionSource = new SessionSource(cfg.BuildConfiguration()
				.Properties, new TestModel());
			Session = SessionSource.CreateSession();
			SessionSource.BuildSchema(Session);
		}

		[TestCleanup]
		public void MyTestCleanup()
		{
			Session.Close();
			Session.Dispose();
		}


		[TestMethod]
		public void TestProjectMapWorksCorrectly()
		{
			new PersistenceSpecification<Project>(Session)
				.CheckProperty(c => c.Name, "John")
				//.CheckProperty(c => c.Tasks, null)
				.VerifyTheMappings();
		}

		[TestMethod]
		public void TestTaskMapWorksCorrectly()
		{
			var project = new Project
			{
				Name = "Apples"
            };
			new PersistenceSpecification<Task>(Session)
                .CheckProperty(p => p.Name, "test")
                .CheckReference(p => p.Project, project)
                .VerifyTheMappings();
		}
	}
}